﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LandandSurvey.BLL
{
    public class Manager
    {
        landsdbEntities context = new landsdbEntities();

        public List<Surveyor> GetSurveyors( bool all )
        {
            if ( all == true )
            {
                var result = from i in context.Surveyors
                             orderby i.Name
                             select i;

                return result.ToList();
            }
            else
            {
                var result = from i in context.Surveyors
                             where i.Status == true
                             orderby i.Name
                             select i;

                return result.ToList();
            }
        }

        public List<County> GetCountys( bool all )
        {
            if ( all == true )
            {
                var result = from i in context.Counties
                             orderby i.Ward
                             select i;

                return result.ToList();
            }
            else
            {
                var result = from i in context.Counties
                             where i.Status == true
                             orderby i.Ward
                             select i;

                return result.ToList();
            }
        }

        public void AddSurveyor( Surveyor surveyor )
        {
            context.Surveyors.AddObject( surveyor );
            context.SaveChanges();
        }

        public void AddCounty( County county )
        {
            context.Counties.AddObject( county );
            context.SaveChanges();
        }

        public void ChangeSurveyorStatus( int id )
        {
            var result = ( from i in context.Surveyors
                           where i.Id == id
                           select i ).FirstOrDefault();

            if ( result.Status )
            {
                result.Status = false;
            }
            else
            {
                result.Status = true;
            }

            context.Surveyors.ApplyCurrentValues( result );
            context.SaveChanges();
        }

        public void ChangeWardStatus( int id )
        {
            var result = ( from i in context.Counties
                           where i.Id == id
                           select i ).FirstOrDefault();

            if ( result.Status )
            {
                result.Status = false;
            }
            else
            {
                result.Status = true;
            }

            context.Counties.ApplyCurrentValues( result );
            context.SaveChanges();
        }
    }
}